<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学海教育 - 在线教育平台</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            display: flex;
            width: 900px;
            height: 650px;
            background-color: white;
            border-radius: 15px;
            overflow: hidden;
            box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2);
        }

        .left-section {
            flex: 1;
            background: linear-gradient(rgba(106, 17, 203, 0.8), rgba(37, 117, 252, 0.8)),
            url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"><rect fill="%236a11cb" width="50" height="50"/><rect fill="%232575fc" x="50" width="50" height="50"/><rect fill="%232575fc" y="50" width="50" height="50"/><rect fill="%236a11cb" x="50" y="50" width="50" height="50"/></svg>');
            background-size: cover;
            color: white;
            padding: 40px;
            display: flex;
            flex-direction: column;
            justify-content: center;
        }

        .left-section h1 {
            font-size: 32px;
            margin-bottom: 20px;
        }

        .left-section p {
            font-size: 16px;
            line-height: 1.6;
            margin-bottom: 20px;
        }

        .features {
            list-style: none;
            margin-top: 20px;
        }

        .features li {
            margin-bottom: 15px;
            display: flex;
            align-items: center;
        }

        .features li i {
            margin-right: 10px;
            font-size: 18px;
        }

        .right-section {
            flex: 1;
            padding: 40px;
            display: flex;
            flex-direction: column;
            justify-content: center;
        }

        .form-container {
            width: 100%;
        }

        .tabs {
            display: flex;
            margin-bottom: 30px;
            border-bottom: 1px solid #e0e0e0;
        }

        .tab {
            padding: 12px 25px;
            cursor: pointer;
            font-weight: 600;
            color: #888;
            transition: all 0.3s ease;
        }

        .tab.active {
            color: #2575fc;
            border-bottom: 2px solid #2575fc;
        }

        .form {
            display: none;
        }

        .form.active {
            display: block;
            animation: fadeIn 0.5s ease;
        }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(10px); }
            to { opacity: 1; transform: translateY(0); }
        }

        .form h2 {
            margin-bottom: 25px;
            color: #333;
        }

        .input-group {
            margin-bottom: 20px;
            position: relative;
        }

        .input-group label {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-weight: 500;
        }

        .input-group input {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #ddd;
            border-radius: 6px;
            font-size: 15px;
            transition: border 0.3s ease;
        }

        .input-group input:focus {
            border-color: #2575fc;
            outline: none;
        }

        .input-group i {
            position: absolute;
            right: 15px;
            top: 40px;
            color: #999;
        }

        .remember-forgot {
            display: flex;
            justify-content: space-between;
            margin-bottom: 20px;
            font-size: 14px;
        }

        .remember-forgot a {
            color: #2575fc;
            text-decoration: none;
        }

        .btn {
            width: 100%;
            padding: 12px;
            background: linear-gradient(to right, #6a11cb, #2575fc);
            color: white;
            border: none;
            border-radius: 6px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(37, 117, 252, 0.4);
        }

        .social-login {
            margin-top: 25px;
            text-align: center;
        }

        .social-login p {
            margin-bottom: 15px;
            color: #777;
            position: relative;
        }

        .social-login p::before, .social-login p::after {
            content: "";
            position: absolute;
            top: 50%;
            width: 30%;
            height: 1px;
            background-color: #ddd;
        }

        .social-login p::before {
            left: 0;
        }

        .social-login p::after {
            right: 0;
        }

        .social-icons {
            display: flex;
            justify-content: center;
            gap: 15px;
        }

        .social-icon {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 18px;
            cursor: pointer;
            transition: transform 0.3s ease;
        }

        .social-icon:hover {
            transform: translateY(-3px);
        }

        .facebook {
            background-color: #3b5998;
        }

        .google {
            background-color: #dd4b39;
        }

        .twitter {
            background-color: #1da1f2;
        }

        .terms {
            margin-top: 20px;
            font-size: 13px;
            color: #777;
            text-align: center;
        }

        .terms a {
            color: #2575fc;
            text-decoration: none;
        }

        @media (max-width: 768px) {
            .container {
                flex-direction: column;
                height: auto;
                width: 100%;
            }

            .left-section {
                padding: 30px;
            }

            .right-section {
                padding: 30px;
            }
        }
    </style>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
</head>
<body>
<div class="container">
    <div class="left-section">
        <h1>学海教育平台</h1>
        <p>加入百万学习者的行列，探索知识，成就未来。</p>
        <ul class="features">
            <li><i class="fas fa-check-circle"></i> 个性化学习路径</li>
            <li><i class="fas fa-check-circle"></i> 专业名师授课</li>
            <li><i class="fas fa-check-circle"></i> 互动学习社区</li>
            <li><i class="fas fa-check-circle"></i> 随时随地学习</li>
        </ul>
    </div>

    <div class="right-section">
        <div class="form-container">
            <div id="global-message" style="margin-bottom:10px;text-align:center;color:#333;"></div>
            <div class="tabs">
                <div class="tab active" data-tab="login">登录</div>
                <div class="tab" data-tab="register">注册</div>
            </div>

            <form class="form active" id="login-form">
                <h2>欢迎回来</h2>
                <div class="input-group">
                    <label for="login-email">邮箱地址</label>
                    <input type="email" id="login-email" placeholder="请输入您的邮箱" required>
                    <i class="fas fa-envelope"></i>
                </div>

                <div class="input-group">
                    <label for="login-password">密码</label>
                    <input type="password" id="login-password" placeholder="请输入您的密码" required>
                    <i class="fas fa-lock"></i>
                </div>

                <div class="remember-forgot">
                    <label>
                        <input type="checkbox"> 记住我
                    </label>
                    <a href="#">忘记密码?</a>
                </div>

                <button type="submit" class="btn">登录</button>
            </form>

            <form class="form" id="register-form">
                <h2>创建新账户</h2>
                <div class="input-group">
                    <label for="register-name">姓名</label>
                    <input type="text" id="register-name" placeholder="请输入您的姓名" required>
                    <i class="fas fa-user"></i>
                </div>

                <div class="input-group">
                    <label for="register-email">邮箱地址</label>
                    <input type="email" id="register-email" placeholder="请输入您的邮箱" required>
                    <i class="fas fa-envelope"></i>
                </div>
                <div class="input-group">
                    <label for="register-password">密码</label>
                    <input type="password" id="register-password" placeholder="请设置密码" required>
                    <i class="fas fa-lock"></i>
                </div>

                <div class="input-group">
                    <label for="register-confirm-password">确认密码</label>
                    <input type="password" id="register-confirm-password" placeholder="请再次输入密码" required>
                    <i class="fas fa-lock"></i>
                </div>

                <button type="submit" class="btn">注册</button>
                <div id="register-message" style="margin-top:12px;color:#333;text-align:center;"></div>

                <div class="terms">
                    点击"注册"即表示您同意我们的 <a href="#">服务条款</a> 和 <a href="#">隐私政策</a>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        // 从 URL 读取后端重定向传递的消息并展示
        const params = new URLSearchParams(window.location.search);
        const globalMsg = document.getElementById('global-message');
        const msgText = params.get('msg');
        if (msgText) {
            globalMsg.textContent = msgText;
            if (msgText.includes('成功')) {
                globalMsg.style.color = '#2e7d32';
            } else {
                globalMsg.style.color = '#c62828';
            }
        }
        // 切换登录/注册表单
        const tabs = document.querySelectorAll('.tab');
        const forms = document.querySelectorAll('.form');

        tabs.forEach(tab => {
            tab.addEventListener('click', function() {
                const tabId = this.getAttribute('data-tab');

                // 更新活跃标签
                tabs.forEach(t => t.classList.remove('active'));
                this.classList.add('active');

                // 显示对应的表单
                forms.forEach(form => {
                    form.classList.remove('active');
                    if (form.id === `${tabId}-form`) {
                        form.classList.add('active');
                    }
                });
            });
        });

        // 页面加载时检查URL参数中的错误消息
        window.addEventListener('load', function() {
            const urlParams = new URLSearchParams(window.location.search);
            const msgParam = urlParams.get('msg');
            if (msgParam) {
                const globalMsg = document.getElementById('global-message');
                globalMsg.textContent = decodeURIComponent(msgParam);
                globalMsg.style.color = '#c62828';
            }
        });

        // 表单提交事件处理
        const loginForm = document.getElementById('login-form');
        const registerForm = document.getElementById('register-form');

        loginForm.addEventListener('submit', function(e) {
            e.preventDefault();
            const email = document.getElementById('login-email').value;
            const password = document.getElementById('login-password').value;

            const formData = new URLSearchParams();
            formData.append('email', email);
            formData.append('password', password);

            const globalMsg = document.getElementById('global-message');
            globalMsg.textContent = '登录中...';

            fetch('/Login', {
                method: 'POST',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                body: formData.toString(),
                redirect: 'follow'
            }).then(response => {
                if (response.redirected) {
                    const url = new URL(response.url);
                    const msgParam = url.searchParams.get('msg');
                    const roleCode=url.searchParams.get('roleCode')
                    if (msgParam) {
                        // 登录失败，显示错误信息
                        globalMsg.textContent = decodeURIComponent(msgParam);
                        globalMsg.style.color = '#c62828';
                    } else {
                        if (roleCode==='2'){
                            window.location.href = '/HomeStu';
                        }
                        else if (roleCode==='3'){
                            window.location.href = '/HomeTeacher';
                        }
                        else {
                            window.location.href = '/HomeAdmin';
                        }

                    }
                } else {
                    return response.text();
                }
            }).then(text => {
                if (text) {
                    globalMsg.textContent = text;
                    globalMsg.style.color = '#c62828';
                }
            }).catch(err => {
                console.error(err);
                globalMsg.textContent = '登录请求失败，请稍后重试';
                globalMsg.style.color = '#c62828';
            });
        });

        registerForm.addEventListener('submit', function(e) {
            e.preventDefault();
            const name = document.getElementById('register-name').value;
            const email = document.getElementById('register-email').value;
            const password = document.getElementById('register-password').value;
            const confirmPassword = document.getElementById('register-confirm-password').value;

            if (password !== confirmPassword) {
                alert('两次输入的密码不一致，请重新输入!');
                return;
            }

            const formData = new URLSearchParams();
            formData.append('username', name);
            formData.append('email', email);
            formData.append('password', password);

            const msg = document.getElementById('register-message');
            msg.textContent = '提交中...';

            fetch('/Register', {
                method: 'POST',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                body: formData.toString(),
                redirect: 'follow'
            }).then(response => {
                if (response.redirected) {
                    // 处理重定向，从URL中提取消息
                    const url = new URL(response.url);
                    const msgParam = url.searchParams.get('msg');
                    if (msgParam) {
                        msg.textContent = decodeURIComponent(msgParam);
                        if (msgParam.includes('注册成功')) {
                            msg.style.color = '#2e7d32';
                        } else {
                            msg.style.color = '#c62828';
                        }
                    }
                } else {
                    return response.text();
                }
            }).then(text => {
                if (text) {
                    msg.textContent = text;
                    if (text.includes('注册成功')) {
                        msg.style.color = '#2e7d32';
                    } else {
                        msg.style.color = '#c62828';
                    }
                }
            }).catch(err => {
                console.error(err);
                msg.textContent = '请求失败，请稍后重试';
                msg.style.color = '#c62828';
            });
        });
    });
</script>
</body>
</html>